// pages/userEvaluationList.js
import api from '../../utils/api.js'
import { config } from '../../utils/config'
import {
    HTTP
} from '../../utils/http.js'
const http = new HTTP()
const app = getApp()
const paramsG = {
        page: 1,
        size: 10,
    }
    // const newParams0 = Object.assign({}, paramsG) //  下拉加载 page加 1
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        userEvaluationLists: [],
        count: 0, // 评***价 的条数
        hpl: 0, //好评率
        paramsData: {},
        imgUrl: config.imgUrl,
    },

    onLoad: function(options) {
        const shopId = wx.getStorageSync("evalutionListShopId")
        let paramsData = this.data.paramsData;

        paramsData = {
            page: 1,
            size: 10,
            id: shopId
        }
        this.setData({
            paramsData
        })
        http.request({
            url: api.getAppraises,
            data: paramsData,
            success: (res) => {
                console.log(2222222, res)
                if (res.code == 200) {
                    let data = res.data;
                    this.setData({
                        userEvaluationLists: data,
                        count: data.count,
                        hpl: data.hpl.toFixed(2)
                    })
                } else if (res.error_code == 201) {
                    this.setData({
                        appraisesNone: true
                    })
                    console.log(' 暂无评***价')
                }

            }
        })
    },
    previewImg(e) {
        let userEvaluationLists = this.data.userEvaluationLists;
        let topindex = e.currentTarget.dataset.topindex;
        let index = e.currentTarget.dataset.index; //当前 图片数组索引
        let images = userEvaluationLists[topindex].simgs
        console.log('images', images)
        let urls = []
        images.forEach((ele) => {
            urls.push(ele.url)
        })
        wx.previewImage({
            current: urls[index],
            urls: urls
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },


    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    onReachBottom: function() {
        let that = this;
        const shopId = wx.getStorageSync("evalutionListShopId")
        paramsG.id = shopId
            // 页数+1
        paramsG.page += 1;
        console.log('paramsG', paramsG)

        wx.showLoading({
            title: '加载中',
        })

        http.request({
            url: api.getAppraises,
            data: paramsG,
            success: (res) => {
                let userEvaluationLists = that.data.userEvaluationLists;
                let newData = res.data;
                //newData 是一个 对象结构
                if (res.code == 200) {
                    let myNum = paramsG.page
                    for (let i in newData) {
                        console.log(Number(i) + 10 * (myNum - 1))
                        let newKey = Number(i) + 10 * (myNum - 1)
                        if (newData[i] instanceof Object) {
                            userEvaluationLists[newKey] = newData[i]
                        }
                    }
                    this.setData({
                        userEvaluationLists
                    })
                    wx.hideLoading();
                } else if (res.error_code == 201) {

                    console.log('已经到底了')
                    wx.hideLoading();
                }

            }
        })


    },
    onUnload: function() {
        wx.removeStorageSync("evalutionListShopId")
    }
})